import { Controller } from "../../mvc/core/Controller";
import { IView } from "../../mvc/interfaces/IView";
import { FairylandCopyModule } from "../../../modules/FairylandCopyModule";
import { WindowEvent } from "../../component/window/WindowEvent";
import { IFairylandCopyModule } from "../../../modules/interfaces/IFairylandCopyModule";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { DataEvent } from "../events/DataEvent";
import { TaskInfo } from "../model/TaskInfo";
import { STask } from "../../../Message/Game/STask";
import { GameProxy } from "../mvc/GameProxy";
import { ENpcEffect } from "../../../Message/Public/ENpcEffect";
import { GameMapUtil } from "../scene/map/GameMapUtil";
import { ETaskGroup } from "../../../Message/Game/ETaskGroup";
import { NpcEffectRule } from "../rules/NpcEffectRule";
import { Alert } from "../../../com/mui/controls/Alert";
import { RolePlayer } from "../scene/player/entity/RolePlayer";
import { EEntityFightMode } from "../../../Message/Public/EEntityFightMode";
import { Language } from "../../../extend/language/Language";
import { SFairylandCopys } from "../../../Message/Public/SFairylandCopys";
import { NPCInfo } from "../model/NPCInfo";
type int = number;
//class FairylandCopyController
    
    export  class FairylandCopyController extends Controller
    {
       

        protected /* override */ initView(): IView
        {
            if (!this._fairylandCopyModule) 
            {
                this._fairylandCopyModule = new FairylandCopyModule();
                this._fairylandCopyModule.addEventListener(WindowEvent.SHOW, this.onFairylandModuleShow);
            }
            return this._fairylandCopyModule;
        }

        /* internal  */onFairylandModuleShow(arg1: WindowEvent): void
        {
            (view as IFairylandCopyModule).updateGroupBtnState();
            return;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.ServerBackCanGetListAdd, this.taskListNumUpdateHander);
            Dispatcher.addEventListener(EventName.ServerBackCanGetListRefresh, this.taskListNumUpdateHander);
            Dispatcher.addEventListener(EventName.ServerBackTaskAdd, this.taskListNumUpdateHander);
            Dispatcher.addEventListener(EventName.ServerBackTaskListRequest, this.taskListNumUpdateHander);
            Dispatcher.addEventListener(EventName.ServerBackTaskUpdate, this.taskRedrawHander);
            Dispatcher.addEventListener(EventName.FairylandGetCopys, this.onGetCopys);
            Dispatcher.addEventListener(EventName.FairylandEnter, this.onEnter);
            Dispatcher.addEventListener(EventName.FairylandOpenTaskPanel, this.openTaskListPanel);
            Dispatcher.addEventListener(EventName.FairylandCloseTaskPanel, this.closeTaskListPanel);
            Dispatcher.addEventListener(EventName.FairylandGetTask, this.getTaskHander);
            Dispatcher.addEventListener(EventName.FairylandCommitTask, this.onCommitTaskHander);
            NetDispatcher.addCmdListener(ServerCommand.FairylandCopysCommand, this.onFairylandCopysHandler);
            NetDispatcher.addCmdListener(ServerCommand.FairylandEnterCommand, this.onFairylandEnterHandler);
            NetDispatcher.addCmdListener(ServerCommand.FairylandOpenCommand, this.onFairyLandOpenHadnler);
            NetDispatcher.addCmdListener(ServerCommand.FairylandCloseCommand, this.onFairyLandCloseHadnler);
            NetDispatcher.addCmdListener(ServerCommand.Group_CaptainChange, this.onCaptainChange);
            NetDispatcher.addCmdListener(ServerCommand.Group_EnterOrLeaveGroup, this.onEnterOrLeaveGroup);
            return;
        }

        /* internal  */closeTaskListPanel(arg1: Object /* flash.events.Event */): void
        {
            (view as IFairylandCopyModule).closeTaskPanel();
            return;
        }

        /* internal  */taskRedrawHander(arg1: Object /* flash.events.Event */): void
        {
            (view as IFairylandCopyModule).updataTask();
            return;
        }

        /* internal  */onCommitTaskHander(arg1: DataEvent): void
        {
            var loc1=arg1.data as TaskInfo;
            Dispatcher.dispatchEvent(new DataEvent(EventName.TaskQuickEndReq, loc1));
            return;
        }

        /* internal  */taskMsgHandler(arg1: Object): void
        {
            var loc1=null;
            if (arg1 == null) 
            {
                return;
            }
            if (typeof arg1 === "stask") 
            {
                if (arg1.getNpc != 3000029) 
                {
                    return;
                }
            }
            else 
            {
                if (typeof arg1 === "array") 
                {
                    loc1 = arg1[0];
                }
                else 
                {
                    loc1 = arg1.task;
                }
                if (loc1 == null) 
                {
                    return;
                }
                if (loc1.sTask.getNpc != 3000029) 
                {
                    return;
                }
            }
            this.taskRedrawHander(null);
            return;
        }

        /* internal  */getTaskHander(arg1: DataEvent): void
        {
            var loc1=arg1.data as TaskInfo;
            GameProxy.taskProxy.getTaskReq(this.FairyLandNpcInfo.tnpc.npcId, loc1.sTask.code);
            return;
        }

        /* internal  */openTaskListPanel(arg1: Object /* flash.events.Event */): void
        {
            if (!this.FairyLandNpcInfo) 
            {
                this.FairyLandNpcInfo = cache.scene.getNpcInfoByEffectId(ENpcEffect._ENpcEffectCopy129);
            }
            cache.dialog.npcInfo = this.FairyLandNpcInfo;
            if (GameMapUtil.curMapState.isCrossCountry) 
            {
                cache.dialog.updateNpcOperate(this.FairyLandNpcInfo.tnpc.effectCross);
            }
            else 
            {
                cache.dialog.updateNpcOperate(this.FairyLandNpcInfo.tnpc.effectDes);
            }
            NetDispatcher.addCmdListener(ServerCommand.TaskUpdate, this.taskMsgHandler);
            this.taskListNumUpdateHander(null, true);
            return;
        }

        /* internal  */taskListNumUpdateHander(arg1: Object /* flash.events.Event */, arg2: Boolean=false): void
        {
            var loc2=null;
            var loc3=null;
            var loc4=null;
            var loc5=null;
            var loc6=null;
            var loc1=(view as IFairylandCopyModule).taskPanelIsShowing || arg2;
            if (loc1) 
            {
                loc2 = cache.dialog.npcInfo;
                loc3 = cache.task.canGetTaskByNpcId(loc2.tnpc.npcId);
                loc4 = cache.task.getTaskByGroup(ETaskGroup._ETaskGroupFairyland);
                loc5 = cache.task.getTaskByGroup(ETaskGroup._ETaskGroupCrossSea);
                loc4 = loc4.concat(loc5);
                loc6 = loc3.concat(loc4);
                (view as IFairylandCopyModule).showTaskListPanel(loc6);
            }
            return;
        }

        /* internal  */onGetCopys(arg1: DataEvent): void
        {
            (view as IFairylandCopyModule).show();
            NpcEffectRule.registDialogEffectWindow(view);
            GameProxy.fairyland.getFairylandCopys();
            return;
        }

        /* internal  */onEnter(arg1: DataEvent): void
        {
            var e: DataEvent;
            var onAlertHandler: Function;

            var loc1;
            onAlertHandler = null;
            e = arg1;
            onAlertHandler = (arg1: int): void
            {
                var loc1=0;
                if (arg1 == Alert.OK) 
                {
                    loc1 = int(e.data);
                    GameProxy.fairyland.enterFairyland(loc1);
                }
                return;
            }
            if (RolePlayer.instance.entityInfo.entityInfo.fightModel != EEntityFightMode._EEntityFightModePeace) 
            {
                onAlertHandler(Alert.OK);
            }
            else 
            {
                Alert.show(Language.getString(42600), null, Alert.OK | Alert.CANCEL, null, onAlertHandler);
            }
            return;
        }

        /* internal  */onFairylandCopysHandler(arg1: SFairylandCopys): void
        {
            (view as IFairylandCopyModule).updateFairylandList(arg1);
            return;
        }

        /* internal  */onFairylandEnterHandler(arg1: Object): void
        {
            (view as IFairylandCopyModule).hide();
            return;
        }

        /* internal  */onFairyLandOpenHadnler(arg1: Object): void
        {
            (view as IFairylandCopyModule).open();
            return;
        }

        /* internal  */onFairyLandCloseHadnler(arg1: Object): void
        {
            (view as IFairylandCopyModule).close();
            return;
        }

        /* internal  */onCaptainChange(arg1: Object): void
        {
            if (this._fairylandCopyModule) 
            {
                (view as IFairylandCopyModule).updateGroupBtnState();
            }
            return;
        }

        /* internal  */onEnterOrLeaveGroup(arg1: Object): void
        {
            if (this._fairylandCopyModule) 
            {
                (view as IFairylandCopyModule).updateGroupBtnState();
            }
            return;
        }

        private /* var */_fairylandCopyModule: IFairylandCopyModule;

        private /* var */FairyLandNpcInfo: NPCInfo;
    }
